package com.estate.charge.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.estate.bean.ChargeDetail;
import com.estate.charge.service.ChargeDetailService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/chargedetail")
@Slf4j
public class ChargeDetailController {

    @Resource
    private ChargeDetailService chargeDetailService;

    @GetMapping("/list/{page}/{size}")
    public JsonResult list(@PathVariable("page") int page, @PathVariable("size") int size) {
        log.info("page:{},size:{}", page, size);
        JsonResult result = chargeDetailService.listByPage(page, size);
        log.info("result:{}", result);
        return result;
    }
    @PostMapping("/save")
    public JsonResult save(ChargeDetail chargeDetail) {
        log.info("新增chargeDetail:{}", chargeDetail);
        JsonResult result = chargeDetailService.saveChargeDetail(chargeDetail);
        log.info("result:{}", result);
        return result;
    }

    @PutMapping("/update")
    public JsonResult update(ChargeDetail chargeDetail) {
        log.info("修改chargeDetail:{}", chargeDetail);
        JsonResult result = chargeDetailService.updateChargeDetail(chargeDetail);
        log.info("result:{}", result);
        return result;
    }

    @DeleteMapping("/delete")
    public JsonResult delete(@RequestParam("chargeDetailId") String chargeDetailId) {
        log.info("删除chargeDetailId:{}", chargeDetailId);
        JsonResult result = chargeDetailService.deleteChargeDetail(chargeDetailId);
        log.info("result:{}", result);
        return result;
    }

//    @GetMapping("/get/{chargeDetailId}")
//    public JsonResult get(@PathVariable("chargeDetailId") String chargeDetailId) {
//        log.info("查询chargeDetailId:{}", chargeDetailId);
//        JsonResult result = ResultTool.success(chargeDetailService.getById(chargeDetailId));
//        log.info("result:{}", result);
//        return result;
//    }


    @GetMapping("/list/{chargeDetailId}")
    public JsonResult list(@PathVariable("chargeDetailId") String chargeDetailId) {
        log.info("查询chargeDetailId:{}", chargeDetailId);
        JsonResult result = ResultTool.success(chargeDetailService.getById(chargeDetailId));
        log.info("result:{}", result);
        return result;
    }

    @GetMapping("/findByCommunityId/{communityId}")
    public Long findByCommunityId(@PathVariable("communityId") String communityId) {
        log.info("查询communityId:{}", communityId);
        QueryWrapper<ChargeDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("community_id", communityId);
        long count = chargeDetailService.count(wrapper);
        log.info("result:{}", count);
        return count;
    }

}
